//
//  PictureCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/28.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "PictureCollectionViewCell.h"
#import "PictureTableViewCell.h"
#import "PictureModel.h"
#import "PictureDetailViewController.h"
#import "PictureDetailModel.h"


@interface PictureCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSArray *dataArr;

@end


@implementation PictureCollectionViewCell

-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    [_navi release];

    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}


- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 40 - 49) style:(UITableViewStylePlain)];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    self.tableV.rowHeight = 200;
    [self.tableV registerClass:[PictureTableViewCell class] forCellReuseIdentifier:@"PictureTableViewCell"];
    [self.contentView addSubview:self.tableV];
    [_tableV release];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [self dataHandle];
}
- (void)dataHandle
{
    NSString *str = @"http://agent1.pconline.com.cn:8941/pcautophoto/iphone_cate_json.jsp?id=89";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.dataArr = [PictureModel transformWithArray:responseObject[@"groups"]];
        [_tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 点击详情方法(点击以后进入一个新的VC)(通过传值id 进入详情界面)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureDetailViewController *PDetailVC = [[PictureDetailViewController alloc] init];
    PictureModel *picModel = self.dataArr[indexPath.item];
    
    
    PDetailVC.mId = [NSString stringWithFormat:@"%@", picModel.mId];
    [self.navi pushViewController:PDetailVC animated:YES];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureTableViewCell" forIndexPath:indexPath];
    PictureModel *pictueM = self.dataArr[indexPath.row];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:pictueM.cover]];
    cell.nameLab.text = pictueM.name;
    cell.countLab.text = [NSString stringWithFormat:@"%@张", pictueM.photoCount];
    
    return cell;
}


@end
